<?php

// +----------------------------------------------------------------------
// | Static Plugin for ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2023 ThinkAdmin [ thinkadmin.top ]
// +----------------------------------------------------------------------
// | 官方网站: https://thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// | 免责声明 ( https://thinkadmin.top/disclaimer )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/think-plugs-static
// | github 代码仓库：https://github.com/zoujingli/think-plugs-static
// +----------------------------------------------------------------------

namespace app\index\controller;


use think\facade\Db;
use app\data\model\DataXinSite;
use app\data\model\DataXinProgram;
use think\facade\Session;

class Index extends Base
{
    public function index()
    {
        $site_list = DataXinSite::select()->toArray();
        $this->assign('site_list',$site_list);
        $this->siteFetch('index');
    }

    public function list()
    {
        $id = $this->request->get('id');
        $key = $this->request->get('key');
        $site_list = DataXinSite::where('id',$id)->find();

        $where[] = ['title','like','%'.$key.'%'];
        $program_list = DataXinProgram::where('cid',$id)
            ->where($where)
            ->order('like','desc')
            ->select()
            ->toArray();

        $this->assign('site_list',$site_list);
        $this->assign('program_list',$program_list);
        $this->siteFetch('list');
    }

    public function details()
    {
        $id = $this->request->get('id');

        $program_list = DataXinProgram::where('id',$id)->find();
        $img = DataXinSite::where('id',$program_list['cid'])->value('img');

        $rank_list = DataXinProgram::where('cid',$program_list['cid'])->order('like','desc')->select()->toArray();

        $this->assign('img',$img);
        $this->assign('program_list',$program_list);
        $this->assign('rank_list',$rank_list);
        $this->siteFetch('details');
    }

    public function voteOp(){
        $id = $this->request->post('id');//节目id
        $type = $this->request->post('type');
        $user_info = Session::get('user_info');
        $url = $_SERVER['HTTP_HOST'] . '/index/index/'. $type . '.html?id=' . $id;
        if(empty($user_info)){
            Session::set('url',$url);
            $list = [
                'code' => 0,
                'msg' => '没有登录,请登录',
                'data' => $url,
            ];
            return json($list);
        }
        $uid = $user_info['id'];

        $num = Db::table('data_xin_like')
                ->where('uid',$uid)
                ->where('pid',$id)
                ->whereDay('addtime')
                ->count();

        if($num >= 5){
            $list = [
                'code' => 2,
                'msg' => '已投完5次，请明天再投',
            ];
            return json($list);
        }
        $like_data = [
            'uid' => $uid,
            'pid' => $id,
            'addtime' => date('Y-m-d H:i:s'),
        ];
        Db::startTrans();
        try {
            Db::table('data_xin_like')->insert($like_data);
            DataXinProgram::where('id',$id)->inc('like')->update();
            Db::commit();
        } catch (\Exception $e){
            Db::rollback();
            $list = [
                'code' => 2,
                'msg' => '投票失败',
            ];
            return json($list);
        }
        $list = [
            'code' => 1,
            'msg' => '投票成功',
        ];
        return json($list);
    }

    public function reg()
    {
        $this->siteFetch('reg');
    }

    public function login()
    {
        $this->siteFetch('login');
    }

    public function loginOp(){
        $name= $this->request->post('name');
        $pwd= $this->request->post('pwd');
        $info = Db::table('data_xin_user')->where('name',$name)->find();
        if(empty($info)){
            $list = [
                'code' => 0,
                'msg' => '没有该账号',
            ];
            return json($list);
        }
        if($info['pwd'] !== $this->md5_password($pwd)){
            $list = [
                'code' => 0,
                'msg' => '密码错误',
            ];
            return json($list);
        }
        Session::set('user_info',$info);
        $url = !empty(Session::get('url')) ? Session::get('url') : '';
        $list = [
            'code' => 1,
            'msg' => '登录成功',
            'data' => $url,
        ];
        return json($list);
    }

    public function regOp()
    {
        $name= $this->request->post('name');
        $pwd= $this->request->post('pwd');
        $info = Db::table('data_xin_user')->where('name',$name)->find();
        if(!empty($info)){
            $list = [
                'code' => 0,
                'msg' => '该账号已注册',
            ];
            return json($list);
        }
        $data = [
            'name' => $name,
            'pwd' => $this->md5_password($pwd),
            'addtime' => date('Y-m-d H:i:s'),
        ];
        Db::table('data_xin_user')->insert($data);
        $list = [
            'code' => 1,
            'msg' => '账号注册成功，请登录',
            'data' => $data,
        ];
        return json($list);
    }

    public function md5_password($password, $hash = 'autofelix_')
    {
        return md5($hash . $password);
    }
}
